/*
 * Copyright (c) 2017 <l_iupeiyu@qq.com> All rights reserved.
 */

package com.uyayo.oms.config;

import com.uyayo.core.interceptor.LocaleChangeInterceptor;
import com.uyayo.core.interceptor.SystemEventInterceptor;
import com.uyayo.framework.excelhelper.service.BaseExportExcelService;
import com.uyayo.framework.excelhelper.service.impl.BaseExportExcelServiceImpl;
import com.uyayo.framework.excelhelper.service.impl.BaseExportExcelXSSFServiceImpl;
import com.uyayo.framework.web.resolver.RequestInfoDefaultArgumentResolver;
import com.uyayo.framework.web.support.RequestDefaultHelper;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.DeviceWebArgumentResolver;
import org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;

import java.util.List;
import java.util.Locale;

@EnableWebMvc//启动MVC
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Autowired
	private MessageSource messageSource;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/");

    }


    @Bean
    public LocaleResolver localeResolver() {
    	SessionLocaleResolver slr = new SessionLocaleResolver();
        //设置默认区域,
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }
    
    /**
     *  校验器 
     */
    @Bean
    public LocalValidatorFactoryBean validator() {
    	LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    	localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
    	localValidatorFactoryBean.setValidationMessageSource(messageSource);
        return localValidatorFactoryBean;
    }
    
    
    @Override
	public Validator getValidator() {
		return validator();
	}


	/**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(systemEventInterceptor())
                .addPathPatterns("/system/**","/cms/**","/ucenter/**","/notepad/**");
        registry.addInterceptor(new LocaleChangeInterceptor())
                //添加需要验证登录用户操作权限的请求
                .addPathPatterns("/**");


    }

    @Bean
    public SystemEventInterceptor systemEventInterceptor(){
        return new SystemEventInterceptor();
    }


    @Bean
    public RequestDefaultHelper requestDefaultHelper(){
        return  new RequestDefaultHelper();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new ServletWebArgumentResolverAdapter(new DeviceWebArgumentResolver()));
        argumentResolvers.add(new ServletWebArgumentResolverAdapter(new SitePreferenceWebArgumentResolver()));
        argumentResolvers.add(new RequestInfoDefaultArgumentResolver(requestDefaultHelper()));

    }


    @Bean(value = "baseExportExcelService")
    public BaseExportExcelService baseExportExcelService() {
        return new BaseExportExcelServiceImpl();
    }

    @Bean(value = "baseExportExcelXSSFService")
    public BaseExportExcelService baseExportExcelXSSFService() {
        return new BaseExportExcelXSSFServiceImpl();
    }

}
